package com.zsrt.day20170504.am.synchronizedtest.threadtest;


/**
 * @author jzh
 * @创建时间 2017年5月4日下午2:25:24 类说明：
 */
public class TicketThread extends Thread {

	private static int ticket = 100;

	@Override
	public void run() {

		for (int i = 1; i <= 100; i++) {
			try {
				//synchronized (TicketThread.class ) {
					sellTicket();
				//}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private synchronized void sellTicket() throws InterruptedException {
		if (ticket >= 1) {
			System.out.println(Thread.currentThread().getName() + "开始售票" + ticket);
			Thread.sleep(10);
			ticket--;
		}
	}

}
/*public class TicketThread extends Thread {
	
	private static int ticket = 100;
	
	@Override
	public void run() {
		
		for (int i = 1; i <= 100; i++) {
			try {
				synchronized (TicketThread.class ) {
					if (ticket >= 1) {
						System.out.println(Thread.currentThread().getName() + "开始售票" + ticket);
						Thread.sleep(10);
						ticket--;
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}
*/